package 每日一题;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/
 */
public class _2047_句子中的有效单词数 {
    /**
     * 分析 不为有效单词的种类：
     * 1、含有数字的、有多个 - 或 标点符号
     * 2、- 位于首位或者末尾
     * 3、标点符号没有置于末尾
     * @param sentence 目标语句
     * @return int
     */
    public int countValidWords(String sentence) {
        String[] target = sentence.split(" ");
        int n = target.length;
        int ans = 0;
        for (String s : target) {
            String word = s.trim();
            if (word.length() != 0) {
                if (check(word)) {
                    ans++;
                }
            }
        }
        return ans;
    }
    public boolean check(String word) {
        boolean flag1 = true;
        boolean flag2 = true;
        if (word.length() == 1 && (word.charAt(0) == ',' || word.charAt(0) == '.' || word.charAt(0) == '!')) {
            return true;
        }
        for (int j = 0; j < word.length(); j++) {
            // - 没有位于首位 或者 末尾
            if (word.contains("-") && (word.startsWith("-") || word.endsWith("-"))) {
                return false;
            }
            // 标点符号没有位于末尾
            if ((word.contains(",") && !word.endsWith(",")) || (word.contains(".") && !word.endsWith(".")) || (word.contains("!") && !word.endsWith("!"))) {
                return false;
            }
            char c = word.charAt(j);
            if (c >= '0' && c <= '9') {
                // 存在数字
                return false;
            } else if (c == '-') {
                // 如果存在多个 -
                if (flag1) {
                    flag1 = false;
                } else {
                    return false;
                }
                // 判断两端是否为字母
                char left = word.charAt(j - 1);
                char right = word.charAt(j + 1);
                if (!(left >= 'a' && left <= 'z') || !(right >= 'a' && right <= 'z')) {
                    return false;
                }
            } else if (c == ',' || c == '.' || c == '!') {
                // 存在多个标点
                if (flag2) {
                    flag2 = false;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 简便写法
     */
    public int countValidWords2(String sentence) {
        String[] ss = sentence.split(" ");
        int ans = 0;
        for (String s : ss) {
            if (check2(s)) {
                ans++;
            }
        }
        return ans;
    }
    boolean check2(String s) {
        int n = s.length();
        if (n == 0) {
            return false;
        }
        for (int i = 0, c1 = 0, c2 = 0; i < n; i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                return false;
            }
            if (c == ' ') {
                return false;
            }
            if (c == '-' && ++c1 >= 0) {
                if (c1 > 1 || (i == 0 || i == n - 1)) {
                    return false;
                }
                if (!Character.isLetter(s.charAt(i - 1)) || !Character.isLetter(s.charAt(i + 1))) {
                    return false;
                }
            }
            if ((c == '!' || c == '.' || c == ',') && ++c2 >= 0) {
                if (c2 > 1 || (i != n - 1)) {
                    return false;
                }
            }
        }
        return true;
    }
}
